package com.ruoyi.hotel.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.hotel.domain.RepairManage;
import com.ruoyi.hotel.mapper.RepairManageMapper;
import com.ruoyi.hotel.service.IRepairManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.mapper.DeviceInfoMapper;
import com.ruoyi.hotel.domain.DeviceInfo;
import com.ruoyi.hotel.service.IDeviceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

import static com.ruoyi.common.utils.SecurityUtils.getAuthentication;

/**
 * 设备信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-04
 */
@Service
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements IDeviceInfoService
{
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Autowired
    private IRepairManageService repairManageService;

    /**
     * 查询设备信息
     * 
     * @param id 设备信息主键
     * @return 设备信息
     */
    @Override
    public DeviceInfo selectDeviceInfoById(String id)
    {
                return getById(id);
    }

    /**
     * 查询设备信息列表
     * 
     * @param deviceInfo 设备信息
     * @return 设备信息
     */
    @Override
    public List<DeviceInfo> selectDeviceInfoList(DeviceInfo deviceInfo)
    {
        return deviceInfoMapper.selectDeviceInfoList(deviceInfo);
    }

    /**
     * 新增设备信息
     * 
     * @param deviceInfo 设备信息
     * @return 结果
     */
    @Override
    public int insertDeviceInfo(DeviceInfo deviceInfo)
    {
        String deviceStatus = deviceInfo.getDeviceStatus();
        if (deviceStatus.equals("1")) {
            deviceInfo.setDeviceStatusText("正常");
        }
        if (deviceStatus.equals("2")) {
            deviceInfo.setDeviceStatusText("损坏");
        }
        if (deviceStatus.equals("3")) {
            deviceInfo.setDeviceStatusText("正在维修");
        }
        deviceInfo.setCreateTime(DateUtils.getNowDate());
                return save(deviceInfo) ? 1 : 0;
    }

    /**
     * 修改设备信息
     * 
     * @param deviceInfo 设备信息
     * @return 结果
     */
    @Override
    public int updateDeviceInfo(DeviceInfo deviceInfo)
    {
        LoginUser user = (LoginUser) getAuthentication().getPrincipal();
        String deviceStatus = deviceInfo.getDeviceStatus();
        if (deviceStatus.equals("1")) {
            deviceInfo.setDeviceStatusText("正常");
        }
        if (deviceStatus.equals("2")) {
            deviceInfo.setDeviceStatusText("损坏");
            RepairManage repairManage = new RepairManage();
            repairManage.setDeviceId(repairManage.getId());
            UUID uuid = UUID.fastUUID();
            repairManage.setId(uuid.toString());
            repairManage.setDeviceStatus("2");
            repairManage.setDeviceStatusText("损坏");
            repairManage.setRepairStatus("1");
            repairManage.setRepairStatus("2");
            repairManage.setRepairStatusText("未受理");
            repairManage.setCreateTime(DateUtils.getNowDate());
            repairManage.setCreateBy(user.getUserId().toString());
            repairManageService.save(repairManage);
        }
        if (deviceStatus.equals("3")) {
            deviceInfo.setDeviceStatusText("正在维修");
        }
        deviceInfo.setUpdateTime(DateUtils.getNowDate());
                return updateById(deviceInfo) ? 1 : 0;
    }

    /**
     * 批量删除设备信息
     * 
     * @param ids 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceInfoByIds(String[] ids)
    {
                return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除设备信息信息
     * 
     * @param id 设备信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceInfoById(String id)
    {
                return removeById(id) ? 1 : 0;
    }
}
